cmake_minimum_required(VERSION 3.2)

include(common/cmakes/cpuinfo.cmake)
include(common/cmakes/bolt.cmake)

project(cheetah C CXX)

set_policy()
set(BOLT_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${BOLT_ROOT}/common/cmakes")
if (USE_CAFFE OR USE_ONNX OR USE_FLOW)
    find_package(Protobuf)
endif()
if (USE_TFLITE)
    find_package(TFLite)
endif (USE_TFLITE)
if (USE_TENSORFLOW)
    find_package(jsoncpp)
endif (USE_TENSORFLOW)
if (USE_GPU)
    find_package(Gcl)
endif(USE_GPU)
if (NOT ANDROID)
    find_package(JNI)
    if (JNI_FOUND)
        set(USE_API_JAVA ON)
    endif(JNI_FOUND)
endif ()
if (USE_SECURE_C)
    find_package(SecureC)
endif ()
if (BUILD_EXAMPLE)
    find_package(jpeg)
    if (EXISTS ${OpenCV_CMAKE_PATH})
        include(${OpenCV_CMAKE_PATH})
    endif()
endif ()

add_subdirectory(common)
if (USE_CAFFE OR USE_ONNX OR USE_TFLITE OR USE_TENSORFLOW OR USE_MINDSPORE)
    add_subdirectory(model_tools)
endif()
add_subdirectory(compute)
add_subdirectory(inference)
if (USE_TRAINING)
    set(TRAINING_BUILD_C_API ON)
    set(TRAINING_BUILD_DEMO ON)
    add_subdirectory(training)
    add_dependencies(Raul blas_enhance uni)
    add_dependencies(Raul blas_enhance_static uni_static)
endif (USE_TRAINING)

#find_program (DOXYGEN_PROGRAM doxygen)
#if (DOXYGEN_PROGRAM)
#    execute_process(COMMAND doxygen .Doxyfile WORKING_DIRECTORY ${BOLT_ROOT})
#else ()
#    message(WARNING "Can not find doxygen tools to generate code documents in docs/API/html/index.html!\
#    If you dont't want to use these documents, you can skip this warning message.")
#endif ()

enable_testing()
find_program (BASH_PROGRAM bash)
if (BASH_PROGRAM AND USE_GENERAL)
    set(parameters --host_dir=${CMAKE_INSTALL_PREFIX})
    if (ANDROID)
        set(parameters ${parameters} -d android)
    elseif("${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "${CMAKE_SYSTEM_PROCESSOR}" AND "${CMAKE_HOST_SYSTEM}" MATCHES "${CMAKE_SYSTEM_NAME}*")
        if ("${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "aarch64" OR "${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "armv7")
            set(parameters ${parameters} -d host)
        elseif (USE_X86)
            set(x86_test ${cpuinfo_avx2})
            if (USE_INT8)
                set(x86_test ${cpuinfo_avx512})
            endif ()
            if (USE_AVX512_VNNI)
                set(x86_test ${cpuinfo_avx512_vnni})
            endif()
            if (x86_test)
                set(parameters ${parameters} -d host)
            else ()
                set(parameters ${parameters} -d unknown)
            endif ()
        else ()
            set(parameters ${parameters} -d unknown)
        endif()
    else()
        set(parameters ${parameters} -d unknown)
    endif()
    if (USE_GPU)
        set(parameters ${parameters} -g)
    endif(USE_GPU)
    if (USE_DYNAMIC_LIBRARY)
        set(parameters ${parameters} --shared)
    endif(USE_DYNAMIC_LIBRARY)
    add_test (NAME quick_benchmark COMMAND bash ${BOLT_ROOT}/scripts/quick_benchmark.sh ${parameters})
endif (BASH_PROGRAM AND USE_GENERAL)

